﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using anaii.EasyNews.Data;
using DotNetNuke.Services.Exceptions;

namespace anaii.EasyNews
{
    public partial class EditCategoriesControl : System.Web.UI.UserControl
    {
        #region Variables

        public bool IsInsertNew
        {
            get
            {
                bool retVal = true;
                if (ViewState["IsInsertNew"] != null && bool.TryParse(ViewState["IsInsertNew"].ToString(), out retVal))
                {
                    return retVal;
                }
                return true;
            }
            set
            {
                ViewState["IsInsertNew"] = value;
            }
        }
        public int EditTypeID
        {
            get
            {
                int retVal = -1;
                if (ViewState["EditTypeID"] != null && int.TryParse(ViewState["EditTypeID"].ToString(), out retVal))
                {
                    return retVal;
                }
                return -1;
            }
            set
            {
                ViewState["EditTypeID"] = value;
            }
        }
        public string CurrentCultureCode
        {
            get
            {
                if (ViewState["CurrentCultureCode"] != null && ViewState["CurrentCultureCode"].ToString().Length > 0)
                {
                    return ViewState["CurrentCultureCode"].ToString();
                }
                return System.Threading.Thread.CurrentThread.CurrentCulture.Name;
            }
            set
            {
                ViewState["CurrentCultureCode"] = value;
            }
        }

        #endregion

        #region Base

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUI(null);
                Visible = false;
            }
        }

        #endregion

        #region Private Methods

        private void BindUI(EasyNewsType currentNewsType)
        {
            if (currentNewsType != null && currentNewsType.TypeID > 0)
            {
                dnn_EasyNewsTypesTranslation translation = currentNewsType.dnn_EasyNewsTypesTranslations.FirstOrDefault(p => p.Culture == CurrentCultureCode);
                if (translation == null)
                {
                    translation = new dnn_EasyNewsTypesTranslation();
                    translation.Culture = CurrentCultureCode;
                    translation.TypeID = currentNewsType.TypeID;
                    currentNewsType.dnn_EasyNewsTypesTranslations.Add(translation);
                }
                txtTitle.Text = currentNewsType.Name;
                txtLocalizedTitle.Text = translation.TypeName;
            }
            else
            {
                if (this.Visible == false)
                    Clear(true);
                else
                    Clear(false);
            }
        }

        private void BindEntity(EasyNewsType currentNewsTypes)
        {
            dnn_EasyNewsTypesTranslation translation = new dnn_EasyNewsTypesTranslation();
            if (currentNewsTypes.dnn_EasyNewsTypesTranslations == null || currentNewsTypes.dnn_EasyNewsTypesTranslations.Count == 0 || currentNewsTypes.dnn_EasyNewsTypesTranslations.FirstOrDefault(p => p.Culture == CurrentCultureCode) == null)
            {
                translation.Culture = CurrentCultureCode;
                translation.TypeID = currentNewsTypes.TypeID;
                currentNewsTypes.dnn_EasyNewsTypesTranslations.Add(translation);
            }
            else
            {
                translation = currentNewsTypes.dnn_EasyNewsTypesTranslations.FirstOrDefault(p => p.Culture == CurrentCultureCode);
            }

            translation.TypeName = txtLocalizedTitle.Text;
            currentNewsTypes.Name = txtTitle.Text;
        }

        #endregion

        #region Public Methods

        public bool Save(int userid)
        {
            bool retVal = false;
            Page.Validate();
            if (Page.IsValid)
            {
                EasyNews.Data.EasyNewsDBDataContext db = new anaii.EasyNews.Data.EasyNewsDBDataContext();
                EasyNewsType currentNewsType = new EasyNewsType();
                if (IsInsertNew)
                {
                    try
                    {
                        BindEntity(currentNewsType);
                        db.EasyNewsTypes.InsertOnSubmit(currentNewsType);
                        db.SubmitChanges();
                        retVal = true;
                    }
                    catch (Exception ex)
                    {
                        Exceptions.LogException(ex);
                        return false;
                    }
                }
                else
                {
                    currentNewsType = db.EasyNewsTypes.First(p => p.TypeID == EditTypeID);
                    if (currentNewsType != null)
                    {
                        try
                        {
                            BindEntity(currentNewsType);
                            db.SubmitChanges();
                            retVal = true;
                        }
                        catch (Exception ex)
                        {

                            Exceptions.LogException(ex);
                            return false;
                        }
                    }
                }

            }
            return retVal;
        }

        public void Clear(bool hide)
        {
            this.Visible = !hide;
            txtTitle.Text = "";
            txtLocalizedTitle.Text = "";
           
        }

        public void RebindCulture(string Culture)
        {
            CurrentCultureCode = Culture;
            EasyNews.Data.EasyNewsDBDataContext db = new anaii.EasyNews.Data.EasyNewsDBDataContext();
            EasyNewsType currentNewsType = new EasyNewsType();
            if (IsInsertNew)
            {
                BindUI(currentNewsType);
            }
            else
            {
                currentNewsType = db.EasyNewsTypes.First(p => p.TypeID == EditTypeID);
                if (currentNewsType != null)
                {
                    BindUI(currentNewsType);
                }
            }
        }

        public void BindData(EasyNewsType currentNewsType)
        {
            IsInsertNew = true;
            EditTypeID = -1;
            Clear(false);
            if (currentNewsType != null)
            {
                BindUI(currentNewsType);
                if (currentNewsType.TypeID > 0)
                {
                    IsInsertNew = false;
                    EditTypeID = currentNewsType.TypeID;
                }
            }
        }

        #endregion
    }
}